package com.gang.config;

import com.gang.listener.ConsumerMessageListener;
import javax.jms.Destination;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

/**异步
 * @ClassName JmsConfig
 * @Description
 * @date 2020-06-19 10:45
 */
//@Configuration
public class ActiveMqConfigAsyn {

  @Value("${topic.name}")
  private String topicName;

  @Value("${spring.activemq.user}")
  private String usrName;

  @Value("${spring.activemq.password}")
  private  String password;

  @Value("${spring.activemq.broker-url}")
  private  String brokerUrl;

  @Bean
  public ActiveMQConnectionFactory connectionFactory() {
    ActiveMQConnectionFactory activeMQConnectionFactory =
        new ActiveMQConnectionFactory(usrName,
        password, brokerUrl);
//    activeMQConnectionFactory.setDispatchAsync(true);
    return activeMQConnectionFactory;
  }

  @Bean
  public DefaultMessageListenerContainer listenerContainer(ActiveMQConnectionFactory connectionFactory){
    DefaultMessageListenerContainer messageListenerContainer =new DefaultMessageListenerContainer();
    messageListenerContainer.setConnectionFactory(connectionFactory);
    Destination d = new ActiveMQTopic("MyTopic");//*表示通配所有队列名称
    messageListenerContainer.setDestination(d);
    messageListenerContainer.setMessageListener(new ConsumerMessageListener());
    return messageListenerContainer;

  }

}
